/*
 * @(#)ListTag.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.web.taglibs.pager;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Web Platform Development Team
 * @jsp.tag name="list" body-content="JSP"
 * @deprecated backward portability
 */
public class ListTag extends BodyTagSupport {
	static final long serialVersionUID = 4774742730570684796L;

	static Log log = LogFactory.getLog(NavigatorTag.class);

	@SuppressWarnings("unchecked")
	private List list;

	@SuppressWarnings("unchecked")
	private Iterator iterator;

	@SuppressWarnings("unchecked")
	Map item;

	/**
	 * @throws JspException JspException
	 * @return int
	 * */
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();

		if (list == null) {
			list = (List)req.getAttribute("list");
		}
		if (list == null) {
			throw new JspException("pager:list tag error - list to show must be given.");
		}
		
		iterator = list.listIterator();
		
		if (!iterator.hasNext()) {
			return SKIP_BODY;
		}
		
		return EVAL_BODY_AGAIN;
	}

	/**
	 * @throws JspException JspException
	 * */
	@SuppressWarnings("unchecked")
	public void doInitBody() throws JspException {
		if (iterator.hasNext()) {
			item = (Map)iterator.next();
		}
	}

	/**
	 * @jsp.attribute required="false" rtexprvalue="true"
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}

	/**
	 * @throws JspException JspException
	 * @return int
	 * */
	public int doEndTag() throws JspException {
		list = null;
		iterator = null;
		item = null;

		BodyContent bodyContent = getBodyContent();
		try {
			if (bodyContent != null) {
				bodyContent.getEnclosingWriter().write(bodyContent.getString());
				bodyContent.clear();
			}
		} catch (IOException e) {
			throw new JspException(e.getMessage(), e);
		}
		return EVAL_PAGE;
	}

	/**
	 * @throws JspException JspException
	 * @return int
	 * */
	@SuppressWarnings("unchecked")
	public int doAfterBody() throws JspException {
		if (iterator.hasNext()) {
			item = (Map)iterator.next();
			return EVAL_BODY_AGAIN;
		}

		return SKIP_PAGE;
	}
}
